﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace SuiYuan.UI.Controls.Forms
{
    /// <summary>
    /// 该功能参照AduSkin进行开发
    /// </summary>
    public class LayUIPasswordBox : TextBox
    {

        public bool ShowIcon
        {
            get { return (bool)GetValue(ShowIconProperty); }
            set { SetValue(ShowIconProperty, value); }
        }
        public static readonly DependencyProperty ShowIconProperty = DependencyProperty.Register("ShowIcon", typeof(bool), typeof(LayUIPasswordBox), new PropertyMetadata(true));
        public string PlaceHolder
        {
            get { return (string)GetValue(PlaceHolderProperty); }
            set { SetValue(PlaceHolderProperty, value); }
        }
        public static readonly DependencyProperty PlaceHolderProperty = DependencyProperty.Register("PlaceHolder", typeof(string), typeof(LayUIPasswordBox), new PropertyMetadata("请输入密码"));

        public bool Radius
        {
            get { return (bool)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); }
        }

        public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(bool), typeof(LayUIPasswordBox));


        ///// <summary>
        ///// 获取或者设置是否能看见密码
        ///// </summary>
        //[Bindable(true), Description("获取或者设置是否能看见密码")]
        //public bool IsCanSeePassword
        //{
        //    get { return (bool)GetValue(IsCanSeePasswordProperty); }
        //    set { SetValue(IsCanSeePasswordProperty, value); }
        //}

        //public static readonly DependencyProperty IsCanSeePasswordProperty = DependencyProperty.Register("IsCanSeePassword", typeof(bool), typeof(LayUIPasswordBox), new PropertyMetadata(true, IsCanSeePasswordChangedCallback));

        //private static void IsCanSeePasswordChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    LayUIPasswordBox passowrdBox = d as LayUIPasswordBox;
        //    if (passowrdBox != null && passowrdBox.PART_SeePassword != null)
        //    {
        //        passowrdBox.PART_SeePassword.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
        //    }
        //}


        /// <summary>
        /// 获取或者设置当前密码值
        /// </summary>
        [Bindable(true), Description("获取或者设置当前密码值")]
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }

        public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(LayUIPasswordBox), new PropertyMetadata(string.Empty));


        public bool ShowPassword
        {
            get { return (bool)GetValue(ShowPasswordProperty); }
            private set { SetValue(ShowPasswordProperty, value); }
        }

        public static readonly DependencyProperty ShowPasswordProperty = DependencyProperty.Register("ShowPassword", typeof(bool), typeof(LayUIPasswordBox), new PropertyMetadata(false, ShowPasswordChanged));

        private static void ShowPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LayUIPasswordBox passwordBox = d as LayUIPasswordBox;
            if (passwordBox != null)
            {
                passwordBox.SelectionStart = passwordBox.Text.Length + 1;
            }
        }

        /// <summary>
        /// 该属性是为了防止明文转化为密文后，设置Text时，再次触发Text_Changed事件
        /// </summary>
        private bool mIsHandledTextChanged = true;
        private StringBuilder mPasswordBuilder;
        /// <summary>
        /// Toggle按钮
        /// </summary>
        private ToggleButton PART_SeePassword;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.PART_SeePassword = this.GetTemplateChild("PART_SeePassword") as ToggleButton;
            //if (this.PART_SeePassword != null)
            //{
            //    this.PART_SeePassword.Visibility = this.IsCanSeePassword ? Visibility.Visible : Visibility.Collapsed;
            //}
            this.SetEvent();
            //回显密码
            this.SetText(this.ConvertToPasswordChar(this.Password.Length));
            //密码框禁止复制
            this.CommandBindings.Add(new System.Windows.Input.CommandBinding(ApplicationCommands.Copy, CommandBinding_Executed, CommandBinding_CanExecute));
        }

        private void SetEvent()
        {
            this.TextChanged += ZPasswordBox_TextChanged;
            if (this.PART_SeePassword != null)
            {
                this.PART_SeePassword.Checked += (o, e) =>
                {
                    this.SetText(this.Password);
                    this.ShowPassword = true;
                };
                this.PART_SeePassword.Unchecked += (o, e) =>
                {
                    this.SetText(this.ConvertToPasswordChar(this.Password.Length));
                    this.ShowPassword = false;
                };
            }
        }

        private void ZPasswordBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!this.mIsHandledTextChanged)
                return;
            foreach (TextChange c in e.Changes)
            {
                //从密码文中根据本次Change对象的索引和长度删除对应个数的字符
                this.Password = this.Password.Remove(c.Offset, c.RemovedLength);
                //将Text新增的部分记录给密码文
                this.Password = this.Password.Insert(c.Offset, Text.Substring(c.Offset, c.AddedLength));
            }
            if (!this.ShowPassword)
            {
                this.SetText(ConvertToPasswordChar(Text.Length));
            }
            //将光标放到最后面
            this.SelectionStart = this.Text.Length + 1;
        }

        private string ConvertToPasswordChar(int length)
        {
            if (mPasswordBuilder != null)
            {
                mPasswordBuilder.Clear();
            }
            else
            {
                mPasswordBuilder = new StringBuilder();
            }

            for (var i = 0; i < length; i++)
            {
                mPasswordBuilder.Append("●");
            }
            return mPasswordBuilder.ToString();
        }

        private void SetText(string str)
        {
            this.mIsHandledTextChanged = false;
            this.Text = str;
            this.mIsHandledTextChanged = true;
        }


        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.Handled = true;
        }
    }
}
